<template>
  <div class="register-container">
    <el-card class="register-card">
      <template #header>
        <h2 class="register-title">用户注册</h2>
      </template>


      <el-form ref="registerFormRef" :model="registerForm" :rules="registerRules" label-width="80px"
        class="register-form">
        <el-form-item label="用户账号" prop="userAccount">
          <el-input v-model="registerForm.userAccount" placeholder="请输入用户账号" prefix-icon="User" />
        </el-form-item>
        <el-form-item label="用户昵称" prop="username">
          <el-input v-model="registerForm.username" placeholder="请输入用户昵称" prefix-icon="User" />
        </el-form-item>


        <el-form-item label="邮箱" prop="email">
          <el-input v-model="registerForm.email" placeholder="请输入邮箱" prefix-icon="Message" />
        </el-form-item>


        <el-form-item label="密码" prop="password">
          <el-input v-model="registerForm.password" type="password" placeholder="请输入密码" prefix-icon="Lock"
            show-password />
        </el-form-item>


        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input v-model="registerForm.confirmPassword" type="password" placeholder="请再次输入密码" prefix-icon="Lock"
            show-password />
        </el-form-item>


        <el-form-item>
          <el-button type="primary" :loading="loading" @click="handleRegister" class="register-btn">
            注册
          </el-button>
          <el-button @click="$router.push('/login')" class="login-link">
            已有账号？去登录
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>


<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/stores/user'


const router = useRouter()
const userStore = useUserStore()


const registerFormRef = ref()
const loading = ref(false)


const registerForm = reactive({
  userAccount:'',
  username: '',
  email: '',
  password: '',
  confirmPassword: ''
})


const validateConfirmPassword = (rule: any, value: string, callback: any) => {
  if (value !== registerForm.password) {
    callback(new Error('两次输入的密码不一致'))
  } else {
    callback()
  }
}


const registerRules = {
  userAccount: [
    { required: true, message: '请输入用户账号', trigger: 'blur' },
    { min: 3, max: 20, message: '用户账号长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  username: [
    { required: true, message: '请输入用户昵称', trigger: 'blur' },
    { min: 3, max: 20, message: '用户昵称长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email' as const, message: '请输入正确的邮箱格式', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于 6 个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请再次输入密码', trigger: 'blur' },
    { validator: validateConfirmPassword, trigger: 'blur' }
  ]
}


const handleRegister = async () => {
  try {
    await registerFormRef.value.validate()
    loading.value = true


    await userStore.register({
      userAccount:registerForm.userAccount,
      userName: registerForm.username,
      userEmail: registerForm.email,
      userPwd: registerForm.password
    })


    ElMessage.success('注册成功！')
    router.push('/login')
  } catch (error: any) {
    ElMessage.error(error.message || '注册失败')
  } finally {
    loading.value = false
  }
}
</script>


<style scoped lang="scss">
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 80vh;
  padding: 20px;
}


.register-card {
  width: 100%;
  max-width: 500px;
}


.register-title {
  text-align: center;
  margin: 0;
  color: $primary-color;
}


.register-form {
  margin-top: 20px;
}


.register-btn {
  width: 100%;
  margin-bottom: 10px;
}


.login-link {
  width: 100%;
}
</style>